package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 佟媛媛 on 18/10/23.
 */

@Component("I1011")
@Scope("prototype")
public class I1011 extends BaseComponent {

    private static Logger log = LoggerFactory.getLogger(I1011.class);


    /**
     * @auth 佟媛媛
     * @date 18/10/29 下午7:13    资产负债、利润、现金流量三表
     * @desc
     * 建议抓取M-6-6中“受限资产”合计数并计算其与总资产占比数（计算公式：近一年及一期“受限资产”合计数/近一期“资产总计”数，资产总额数可以从M-6-1提取），“资产总计”字段也可以是“总资产”
     * 若该比例超过30%，建议提示“发行人存在受限资产占比较大的风险”
     * "1、在M-6-6章节抽取受限资产总计，总资产从M-6-1获取；
    2、如果受限资产总计不存在，提示“未能抽取到受限资产总计”；
    3、受限资产总计和总资产应为同期数据；
    4、根据受限资产时间和对应年报中的总资产进行比较，e.g. 2017年受限资产/2017年总资产；
    5、如果受限存在近一年及一期共计2条数据，同时提示2条数据的比值；"
     */
    @Override
    public List<ComponentResult> handlerM() {
        return common("M-6");
    }

    private List<ComponentResult> common(String catalogId){
        String exist = "02";
        boolean annotationFlag = true;
        List<ComponentResult> list = new ArrayList<>();
        setCatalogId(catalogId);
        String promptMsg = "缺失";

        int ZCZJ_pageNum = getChapterModel(getCatalogId()).getStartPage();
        KVText ZCZJ_kvText = null;
        List<ComponentResult.TextPosition> ZCZJ_textPositionList = null;

        int SXZC_pageNum = getChapterModel(getCatalogId()).getStartPage();
        KVText SXZC_kvText = null;
        List<ComponentResult.TextPosition> SXZC_textPositionList = null;


        SXZC_kvText = getKVTextValue(getCatalogId(),"SXZC","SXZC");
//        SXZC_textPositionList = getContentAndPositionList(SXZC_kvText);
        if(SXZC_kvText==null){//受限资产不存在
            list.add(getComponentResult(annotationFlag,exist,"受限资产合计与总资产占比数",SXZC_pageNum,"受限资产不存在",getRuleTitle(),SXZC_textPositionList));
            return list;
        }


        double sxzc = getDouble(SXZC_kvText.getText());
        double zczj = 0;
        ExtractBizModel model = getModel(getCatalogId(),"ZCFZ");
        String jyq = getJYQ(model.getTables());
        Map<String, ExtractItem> col = model.getTable(jyq);
        if(MapUtils.isEmpty(col)){//资产总计不存在
            list.add(getComponentResult(annotationFlag,exist,"受限资产合计与总资产占比数",ZCZJ_pageNum,jyq+"资产总计不存在",getRuleTitle(),ZCZJ_textPositionList));
            return list;
        }else{
            ExtractItem item = col.get("ZCZJ");//资产总计
            if(item!=null && item.getValue()!=null){
                ZCZJ_kvText = item.getValue();
//                ZCZJ_textPositionList = getContentAndPositionList(ZCZJ_kvText);
                zczj = getDouble(ZCZJ_kvText.getText());
            }
        }
        double d = sxzc / zczj;
        if(d>0.3){
            exist = "01";
            annotationFlag = false;
            promptMsg = "发行人存在受限资产占比较大的风险";
            list.add(getComponentResult(annotationFlag,exist,"受限资产合计与总资产占比数",SXZC_pageNum,promptMsg,getRuleTitle(),null));
        }else{
            list.add(getComponentResult(annotationFlag,exist,"受限资产合计与总资产占比数",SXZC_pageNum,"受限资产合计与总资产占比数"+getTwoDecimal(d),getRuleTitle(),SXZC_textPositionList));
        }
        return list;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        return common("BM-6");
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return common("DM-6");
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return common("BDM-6");
    }


    private ComponentResult getComponentResult(boolean annotationFlag,String exist,String keyword,int pageNum,String promptMsg,String ruleName,List<ComponentResult.TextPosition> textPositionList){
        ComponentResult result = new ComponentResult();
        result.setAnnotationFlag(annotationFlag);
        result.setCatalogId(getCatalogId());
        result.setChapterId(getChapterId());
        result.setExist(exist);
        result.setKeyword(keyword);
        result.setPageNum(pageNum);
        result.setPromptMessage("01".equals(exist)?"正确":promptMsg);
        result.setRegulationId(getRegulationId());
        result.setRuleName(ruleName);
        result.setTextPosition(textPositionList);
        result.setUid(UUID.randomUUID().toString());
        return result;
    }

}
